<?php
class ome_ctl_admin_dly_corp extends desktop_controller{
    var $name = "物流公司管理";
    var $workground = "setting_tools";
    function index(){
       $this->finder('ome_mdl_dly_corp',array(
            'title'=>'物流公司管理',
            'actions'=>array(
                            array('label'=>'添加物流公司','href'=>'index.php?app=ome&ctl=admin_dly_corp&act=add&finder_id='.$_GET['finder_id'],'target'=>'_blank'),
                        ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
         ));
    }

    function add(){
        $this->pagedata['config']=array(
                'firstunit' => '1000',
                'continueunit'=>'1000'
        );
        $print_tmplObj = &$this->app->model('print_tmpl');
        $this->pagedata['prttpl'] = $print_tmplObj->getList("prt_tmpl_id,prt_tmpl_title");
        $this->pagedata['weightunit'] = $this->_weightunit();
		$this->pagedata['title'] = '添加物流公司';
        $this->singlepage("admin/system/dly_corp.html");
    }

    function editdly_corp($corp_id){

        $oDlycorp = &$this->app->model('dly_corp');
        $dly_info = $oDlycorp->dump($corp_id);
        $dly_info['area_fee_conf'] = unserialize($dly_info['area_fee_conf']);
        $dly_info['protect_rate'] = $dly_info['protect_rate']*100;
        $this->pagedata['dt_info'] = $dly_info;
        $this->pagedata['weightunit'] = $this->_weightunit();
        $print_tmplObj = &$this->app->model('print_tmpl');
        $this->pagedata['prttpl'] = $print_tmplObj->getList("prt_tmpl_id,prt_tmpl_title");
        $this->pagedata['prttmpid'] = $dly_info['prt_tmpl_id'];
		$this->pagedata['title'] = '编辑物流公司';
        $this->singlepage("admin/system/dly_corp.html");
    }
    function saveDlType(){
        $this->begin('index.php?app=ome&ctl=admin_dly_corp');
        $oObj = &$this->app->model('dly_corp');
        $dlycorp = $_POST;

        if($dlycorp['firstprice']<0 || $dlycorp['continueprice']<0){
            $this->end(false, app::get('base')->_('首重费用和续重费用都不能为负！'));
        }
        
        //配送地区
        $area_fee_conf = array();
        if ($dlycorp['area_fee_conf']['areaGroupName'])
        foreach ($dlycorp['area_fee_conf']['areaGroupName'] as $k=>$v){
            $ishave = 0;
            $c['areaGroupName'] = $v;
            $c['areaGroupId'] = $dlycorp['area_fee_conf']['areaGroupId'][$k];
            $c['firstprice'] = $dlycorp['area_fee_conf']['firstprice'][$k];
            $c['continueprice'] = $dlycorp['area_fee_conf']['continueprice'][$k];
            $c['dt_expressions'] = $dlycorp['area_fee_conf']['dt_expressions'][$k];
            $c['dt_useexp'] = $dlycorp['area_fee_conf']['dt_useexp'][$k];

            if($c['firstprice']<0 || $c['continueprice']<0){
                $this->end(false, app::get('base')->_('配送地区的首重费用和续重费用都不能为负！'));
            }

            if (!$c['areaGroupName'] and !$c['areaGroupId'] and !$c['firstprice'] and !$c['continueprice'] and !$c['dt_expressions'] and !$c['dt_useexp']) $ishave = 0;
            else $ishave = 1;
            $area_fee_conf[] = $c;
        }
        if ($dlycorp['firstunit'] <= 0 || $dlycorp['continueunit'] <= 0){
            $this->end(false, app::get('base')->_('首重和续重必须设置并大于0'));
        }
        $dlycorp['area_fee_conf'] = $area_fee_conf;
        $dlycorp['protect'] = $dlycorp['protect']?$dlycorp['protect']:'false';
        $oObj->save($dlycorp);
        
        $dlycorp_area_fee_conf = $dlycorp['area_fee_conf']?unserialize($dlycorp['area_fee_conf']):"";
        $areaObj = &$this->app->model('dly_corp_area');
        $areaObj->delete(array('corp_id'=>$dlycorp['corp_id']));
        if ($dlycorp_area_fee_conf)
        foreach ($dlycorp_area_fee_conf as $key=>$val){
            $areas = $val['areaGroupId'];
            $area_ids = explode(",",$areas);
            if ($area_ids){
                $oObj->set_area($area_ids,$dlycorp['corp_id']);
            }
        }
        
        $this->end(true, app::get('base')->_('保存成功'));
    }
    
    function _weightunit(){
        return array(
            "500"=>app::get('base')->_("500克"),
            "1000"=>app::get('base')->_("1公斤"),
            "1200"=>app::get('base')->_("1.2公斤"),
            "2000"=>app::get('base')->_("2公斤"),
            "5000"=>app::get('base')->_("5公斤"),
            "10000"=>app::get('base')->_("10公斤"),
            "20000"=>app::get('base')->_("20公斤"),
            "50000"=>app::get('base')->_("50公斤")
        );
    }

    function showRegionTreeList($serid,$multi=false){
         if($serid){
         $this->pagedata['sid'] = $serid;
         }else{
         $this->pagedata['sid'] = substr(time(),6,4);
         }
         $this->pagedata['multi'] =  $multi;
         $this->display('admin/system/regionSelect.html');
    }

    function getRegionById($pregionid){
        $oDlyType = &$this->app->model('dly_corp');
        echo json_encode($oDlyType->getRegionById($pregionid));
    }

    function checkExp(){
        $this->pagedata['expressions'] = $_GET['expvalue'];
        $this->display('admin/delivery/check_exp.html');
    }
    
    function corp_recommend(){
        $oDly_corp = &app::get('ome')->model('dly_corp');
        $this->pagedata['corp_list'] = $oDly_corp->corp_default();
        $this->display('admin/system/dly_corp_recommend.html');
    }
}

?>
